package com.kgc.novel.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kgc.novel.common.Res;
import com.kgc.novel.dao.BookRecordMapper;
import com.kgc.novel.domain.entity.BookRecord;
import com.kgc.novel.domain.param.BookRecordParam;
import com.kgc.novel.domain.vo.BookRecordVO;
import com.kgc.novel.domain.vo.BookShelfVO;
import com.kgc.novel.service.user.BookRecordService;
import com.kgc.novel.util.SnowflakeIdWorker;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

@Service
@Transactional(rollbackFor = Exception.class)
public class BookRecordServiceImpl extends ServiceImpl<BookRecordMapper, BookRecord> implements BookRecordService {
    @Resource
    private BookRecordMapper bookRecordMapper;

    @Override
    public Res selectRecordAll(BookRecordParam bookRecordParam) {
        List<BookRecordVO> allShelf = bookRecordMapper.getAllRecord(bookRecordParam);
        Integer countShelf = bookRecordMapper.getCountRecord(bookRecordParam.getNickName());
        return Res.returnData(allShelf, (long) countShelf);
    }

    @Override
    public Res getSingleRecord(String id) {
        BookRecord bookRecord = bookRecordMapper.selectById(id);
        if (bookRecord != null) {
            return Res.returnData("200", bookRecord);
        }
        return Res.returnMsg("暂无用户阅读记录！");
    }

    @Override
    public Res addRecord(BookRecord bookRecord) {
        LambdaQueryWrapper<BookRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookRecord::getUserId, bookRecord.getUserId());

        BookRecord bookRecord1 = bookRecordMapper.selectOne(lambdaQueryWrapper);
        if (bookRecord1 != null) {
            return Res.returnMsg("用户记录已经存在，请重新添加！");
        }
        SnowflakeIdWorker idWorker = new SnowflakeIdWorker(0, 0);
        long id = idWorker.nextId();
        bookRecord.setId(String.valueOf(id));
        bookRecordMapper.insert(bookRecord);
        return Res.returnMsg("添加成功！");
    }

    @Override
    public Res updateRecord(BookRecord bookRecord) {
        LambdaQueryWrapper<BookRecord> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(BookRecord::getUserId, bookRecord.getUserId());

        BookRecord bookRecord1 = bookRecordMapper.selectOne(lambdaQueryWrapper);
        if (bookRecord1 == null) {
            bookRecordMapper.updateById(bookRecord);
            return Res.returnMsg("修改成功！");
        }
        BookRecord bookRecord2 = bookRecordMapper.selectById(bookRecord.getId());
        if (bookRecord2.getId().equals(bookRecord.getId())) {
            bookRecordMapper.updateById(bookRecord);
            return Res.returnMsg("修改成功！");
        }
        return Res.returnMsg("用户记录已经存在，请重新修改！");
    }

    @Override
    public Res deleteRecord(String id) {
        int i = bookRecordMapper.deleteById(id);
        if (i != -1) {
            return Res.returnMsg("删除成功！");
        } else {
            return Res.returnMsg("删除失败！");
        }
    }

    @Override
    public Res deleteAllRecord(List<String> ids) {
        int i = bookRecordMapper.deleteBatchIds(ids);
        if (i != -1) {
            return Res.returnMsg("批量删除成功");
        } else {
            return Res.returnMsg("批量删除失败");
        }
    }
}
